from datetime import datetime
from flask import request

from flaskapp import db
from flaskapp.entity.PersonalBook import PersonalBook
from flaskapp.entity.PsnlRecord import PsnlRecord
from flaskapp.util.Jwt import user_token_required
from flaskapp.util.Redprint import Redprint
from flaskapp.util.Result import Result

api = Redprint('personalbooks')


@api.route('/addPersonalBook', methods=['POST'])
@user_token_required(admin_required=False)
# 添加个人图书馆中的个人图书
def add_personal_book():
    data = request.form
    bookname = data['bookname']
    loanerid = data['loanerid']
    author = data['author']
    press = data['press']
    category = data['category']
    surplus = data['surplus']
    personalBook = PersonalBook(loanerid, bookname, author, press, category, None, surplus)
    personalBook.addPsnlBook()
    return Result.success(None, 'add personal book successfully')


@api.route('/getAllPersonalBook', methods=['GET'])
@user_token_required(admin_required=False)
def get_all_personal_book():
    personalBooks = PersonalBook.query.all()
    books_list = []
    for personalBook in personalBooks:
        books_list.append(personalBook.to_dict())
    return Result.success(books_list)


@api.route('/getPersonalBookByBookname', methods=['GET'])
# 这个用于搜索功能
@user_token_required(admin_required=False)
def get_personal_book_by_user_id():
    data = request.form
    bookname = data['bookname']
    personalBook = PersonalBook.query.filter_by(bookname=bookname).first()
    if personalBook is not None:
        return Result.success(None, personalBook.to_dict())
    else:
        return Result.error(404, 'book not found')


@api.route('/deletePersonalBook', methods=['POST'])
# 用于删除个人书籍
@user_token_required(admin_required=False)
def delete_personal_book():
    data = request.form
    bookname = data['bookname']
    personalBook = PersonalBook.query.filter_by(bookname=bookname).first()
    if personalBook is not None:
        personalBook.deletePersonalBook()
        return Result.success(None, 'delete personal book successfully')
    else:
        return Result.error(404, 'book not found')


@api.route('/borrowPersonalBook', methods=['POST'])
# 用于借书
@user_token_required(admin_required=False)
def borrow_personal_book():
    data = request.form
    borrowerid = data['borrowerid']
    bookname = data['bookname']
    loandays = data['loandays']
    borrowBook = PersonalBook.query.filter_by(bookname=bookname).first()
    loanerid1 = borrowBook.loanerid

    if borrowBook is not None:
        if borrowBook.borrowPersonalBook():
            PsnlRecord(borrowBook.bookid, loanerid1, borrowerid, datetime.now(), loandays).addRecord()
            return Result.success(None, 'borrow personal book successfully')
        else:
            return Result.error(404, 'book not found ')


@api.route('/updatePersonalBook', methods=['PUT'])
# 用于更新个人书记内容
@user_token_required(admin_required=False)
def update_personal_book():
    # 获取 PUT 请求中的数据
    data = request.form
    bookname = data['bookname']

    # 检查数据是否存在
    if not data:
        return Result.error(404, '404 not found')

    personal_book = PersonalBook.query.filter_by(bookname=bookname).first()
    if personal_book:
        # 更新个人图书的属性
        personal_book.author = data.get('author')
        personal_book.press = data.get('press')
        personal_book.category = data.get('category')
        personal_book.coverpath = data.get('coverpath')
        personal_book.surplus = data.get('surplus')
        db.session.commit()

        return Result.success(None, 'Update personal book successfully')
    else:
        return Result.error(404, 'book not found')
